package com.wlz.client;

import com.wlz.common.Meassage;
import com.wlz.common.MeassageType;
import com.wlz.utils.HessianSerializerUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * BIO的客户端实现
 */
public class BioClient {
    public static void main(String[] args) {
        String userName = "fox";
        Scanner in = new Scanner(System.in);
        String input = null;
        Client client = new Client("localhost", 8080, userName);
        System.out.println(userName + "连接成功,请按照 <接收人>:<消息格式> 发送");
        //持续接受服务端发送的报文
        new Thread(client::getResponse).start();

        while ((input = in.next()).length() > 0) {
            try {
                String[] inputs = input.split(":");
                client.request(
                        new Meassage(MeassageType.MEASSAGE.name(), inputs[1], inputs[0]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class Client {
        String host;
        int port;
        String userName;

        public Client(String host, int port, String userName) {
            this.host = host;
            this.port = port;
            this.userName = userName;
            connect();
        }

        Socket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;

        public boolean connect() {
            if (socket != null && inputStream != null && outputStream != null
                    && socket.isBound() && socket.isConnected() && !socket.isClosed()) {
                return true;
            }
            try {
                socket = new Socket("localhost", 8080);
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
                //首次连接 传输用户信息
                request(new Meassage(MeassageType.LOGIN.name(), this.userName));
            } catch (IOException e) {
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
            return false;
        }

        public void request(Meassage request) {
            if (!connect()) {
                return;
            }
            try {
                outputStream.write(HessianSerializerUtil.serialize(request));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public void getResponse() {
            byte[] res = new byte[1024];
            try {
                while (inputStream.read(res) > 0) {
                    Meassage requestMeassage = HessianSerializerUtil.deserialize(res);
                    System.out.println("您有新的新消息->" + requestMeassage.getContext());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}